// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.linqingying.gitcode.pullrequest.ui.list

import com.intellij.collaboration.ui.codereview.list.error.ErrorStatusPresenter
import com.intellij.openapi.project.Project
import org.jetbrains.annotations.Nls
import com.linqingying.gitcode.authentication.accounts.GitCodeAccount
import com.linqingying.gitcode.exceptions.GitCodeAuthenticationException
import com.linqingying.gitcode.i18n.GitCodeBundle
import com.linqingying.gitcode.pullrequest.ui.GCApiLoadingErrorHandler
import com.linqingying.gitcode.ui.component.GCHtmlErrorPanel
import javax.swing.Action

class GCPRErrorStatusPresenter(
  project: Project,
  account: GitCodeAccount,
  resetRunnable: () -> Unit
) : ErrorStatusPresenter.Text<Throwable> {
  private val errorHandler = GCApiLoadingErrorHandler(project, account, resetRunnable)

  override fun getErrorTitle(error: Throwable): @Nls String = GitCodeBundle.message("pull.request.list.cannot.load")

  override fun getErrorDescription(error: Throwable): @Nls String {
    return if (error is GitCodeAuthenticationException) GitCodeBundle.message("pull.request.list.error.authorization")
    else GCHtmlErrorPanel.getLoadingErrorText(error)
  }

  override fun getErrorAction(error: Throwable): Action = errorHandler.getActionForError(error)
}
